package org.apache.solr.update;

import java.io.IOException;
import org.apache.lucene.index.IndexWriter;
import org.apache.solr.core.CoreContainer;
import org.apache.solr.core.DirectoryFactory;
import org.apache.solr.core.SolrCore;
import org.apache.solr.util.RefCounted;

/**
 * The state in this class can be easily shared between SolrCores across
 * SolrCore reloads.
 * 
 */
public abstract class SolrCoreState {

    private final Object deleteLock = new Object();

    public Object getUpdateLock() {
        return deleteLock;
    }

    /**
     * Force the creation of a new IndexWriter using the settings from the given
     * SolrCore.
     *
     * @param rollback close IndexWriter if false, else rollback
     * @throws IOException If there is a low-level I/O error.
     */
    public abstract void newIndexWriter(SolrCore core, boolean rollback) throws IOException;

    /**
     * Get the current IndexWriter. If a new IndexWriter must be created, use
     * the settings from the given {@link SolrCore}.
     *
     * @throws IOException If there is a low-level I/O error.
     */
    public abstract RefCounted<IndexWriter> getIndexWriter(SolrCore core) throws IOException;

    /**
     * Decrement the number of references to this state. When then number of
     * references hits 0, the state will close. If an optional closer is passed,
     * that will be used to close the writer.
     *
     * @throws IOException If there is a low-level I/O error.
     */
    public abstract void decref(IndexWriterCloser closer) throws IOException;

    /**
     * Increment the number of references to this state.
     */
    public abstract void incref();

    /**
     * Rollback the current IndexWriter. When creating the new IndexWriter use
     * the settings from the given {@link SolrCore}.
     *
     * @throws IOException If there is a low-level I/O error.
     */
    public abstract void rollbackIndexWriter(SolrCore core) throws IOException;

    /**
     * @return the {@link DirectoryFactory} that should be used.
     */
    public abstract DirectoryFactory getDirectoryFactory();

    public interface IndexWriterCloser {

        public void closeWriter(IndexWriter writer) throws IOException;
    }

    public abstract void doRecovery(CoreContainer cc, String name);

    public abstract void cancelRecovery();
}
